﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cls.u_Models
{
    public partial class ShowClsLog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //加载Item和Value
            DropDownList_ShowApplicationName();
        }

        protected void Button_ShowLogDetail_Click(object sender, EventArgs e)
        {
            String ApplicationName = DropDownList_ApplicationName.SelectedValue;

            GridView_LogDetail_DataBind(ApplicationName);
        }

        protected void DropDownList_ShowApplicationName()
        {
            DropDownList_ApplicationName.Items.Clear();

            DropDownList_ApplicationName.Items.Add(new ListItem("全部", "all"));

            //SQLServer连接
            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            //打开SQLServer连接
            SqlConnection.Open();

            try
            {
                //定义查询语句
                SqlCommand SelectCommand = new SqlCommand("SELECT DISTINCT [ApplicationName] FROM [dbo].[u_cls_Log] ORDER BY ApplicationName", SqlConnection);

                SqlDataReader ApplicationNameReader = SelectCommand.ExecuteReader();

                while (ApplicationNameReader.Read())
                {
                    DropDownList_ApplicationName.Items.Add(new ListItem(ApplicationNameReader["ApplicationName"].ToString(), ApplicationNameReader["ApplicationName"].ToString()));
                }

                ApplicationNameReader.Close();
                SelectCommand.Dispose();
            }
            catch (SqlException e)
            {
                Label_DebugMsg.Text = "SqlException Message：" + e.Message + "<br />";
                Label_DebugMsg.Text += "SqlException StackTrace：" + e.StackTrace + "<br />";
            }

            SqlConnection.Close();
        }

        protected void GridView_LogDetail_DataBind(String ApplicationName)
        {
            //GridView_BackorderDetail.AutoGenerateColumns = false;
            GridView_LogDetail.Columns.Clear();

            GridView_LogDetail.DataSource = LogDetailDataSet(ApplicationName);

            GridView_LogDetail.DataKeyNames = new string[] { "id" };

            //BoundField bField;

            //bField = new BoundField();
            //bField.DataField = "id";
            //bField.HeaderText = "编号";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
            
            //bField = new BoundField();
            //bField.DataField = "LogDatetime";
            //bField.HeaderText = "记录时间";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
           
            //bField = new BoundField();
            //bField.DataField = "ApplicationName";
            //bField.HeaderText = "程序名";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
            
            //bField = new BoundField();
            //bField.DataField = "ApplicationUserName";
            //bField.HeaderText = "程序用户名";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
            
            //bField = new BoundField();
            //bField.DataField = "LogSource";
            //bField.HeaderText = "记录来源";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
           
            //bField = new BoundField();
            //bField.DataField = "LogMessage";
            //bField.HeaderText = "日志信息";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);
           
            //bField = new BoundField();
            //bField.DataField = "Remark";
            //bField.HeaderText = "标记";
            //bField.ReadOnly = true;
            //GridView_LogDetail.Columns.Add(bField);

            //GridView_LogDetail.Columns[0].Visible = false;

            //显示分页
            //分页必须PageIndexChanging事件
            //GridView_LogDetail.AllowPaging = true;
            //每页显示100条记录
            //GridView_LogDetail.PageSize = 100;

            //列排序参考
            //http://www.cnblogs.com/happygx/archive/2011/04/12/2013417.html
            //http://www.cnblogs.com/heekui/archive/2008/06/02/1212051.html

            GridView_LogDetail.DataBind();
        }

        protected DataSet LogDetailDataSet(String ApplicationName)
        {
            DataSet LogDetailDataSet = new DataSet();

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            String SelectCommandString = "SELECT [id],[LogDatetime],[ApplicationName],[ApplicationUserName],[LogSource],[LogMessage],[Remark] FROM [dbo].[u_cls_Log]";

            if (ApplicationName.Equals("all"))
            { }
            else
            {
                SelectCommandString += " WHERE [ApplicationName] = '" + ApplicationName + "'";
            }

            SelectCommandString += " ORDER BY [LogDatetime] DESC";

            SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectCommandString, SqlConnection);

            try
            {
                dataAdapter.Fill(LogDetailDataSet);
            }
            catch (SqlException e)
            {
                Label_DebugMsg.Text = "SqlException Message：" + e.Message + "<br />";
                Label_DebugMsg.Text += "SqlException StackTrace：" + e.StackTrace + "<br />";
            }
            dataAdapter.Dispose();
            SqlConnection.Close();

            return LogDetailDataSet;
        }
    }
}